from sqlmodel import Field, Relationship, SQLModel

from .base import SQLBaseModel
from .role_menu import RoleMenu
from .user_role import UserRole
from typing import TYPE_CHECKING, Optional, List

if TYPE_CHECKING:
    from .user import User
    from .menu import Menu


class BaseRole(SQLModel):
    name: str = Field(index=True, description="名称")
    auth_key: str = Field(index=True, description="权限字符")
    description: Optional[str] = Field(default=None, description="描述")
    is_enabled: bool = Field(default=True, index=True, description="是否启用")


class Role(SQLBaseModel, BaseRole, table=True):
    __tablename__ = "role"

    """多对多关系"""
    users: List["User"] = Relationship(back_populates="roles", link_model=UserRole)
    menus: List["Menu"] = Relationship(back_populates="roles", link_model=RoleMenu)
